package com.ficus.road.maintain.third.zh.maintenance;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ficus.road.maintain.core.model.zh.ComplainInfo;
import com.ficus.road.maintain.dao.ComplainDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.List;

/**
 * @author jingyan ma
 * @date 2023/8/8 下午3:00
 */
@Component
public class ZHComplain extends BaseZH {

    @Autowired
    private ComplainDao complainDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangRoadDiseaseData";
        arg1 = "{\"startTime\":\""+ LocalDate.now().minusDays(1)+"\",\"endTime\":\""+LocalDate.now()+"\",\"typeflag\":\"0\"}";
//        arg1 = "{\"startTime\":\"2021-09-01\",\"endTime\":\"2021-09-01\",\"typeflag\":\"0\"}";

        errMsg = "调用中海接口获取道路病害数据异常!";
        successMsg = "调用中海接口获取道路病害数据成功!";
    }
    //    @Scheduled(cron = "0 1/3 * * * ?")
    @Scheduled(cron = "0 0 12,14,16 * * ?")
    public void refreshBasicBridge() {
        init();
        String basicBridgeStr = null;
        int i = 0;
        while (i < 10) {
            basicBridgeStr = getZHData();
            if (ObjectUtils.isNotEmpty(basicBridgeStr)) {
                break;
            }
            i++;
        }
        if (ObjectUtils.isEmpty(basicBridgeStr)) {
            return;
        }
        List<ComplainInfo> bridgeList = JSON.parseObject(basicBridgeStr, new TypeReference<List<ComplainInfo>>() {});

        bridgeList.parallelStream().forEach(item -> {
            complainDao.saveOrUpdate(item);
        });
    }
}
